package com.gack.business.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gack.business.dao.UserInfonobind;
import com.gack.business.dao.UserManageDao;
import com.gack.business.dao.UserManageInfoDao;
import com.gack.business.dao.Usernobind;
import com.gack.business.model.User;
import com.gack.business.repository.UserRepository;
import com.gack.business.vo.UserInfoEnterpriseVO;
import com.gack.business.vo.UserInfoOrdersVO;

/**
 * 
* @ClassName: UserService 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年6月19日 下午2:47:05 
* @version V1.0
 */
@Service
public class UserService implements UserServiceInterface{

	@Autowired
	private UserManageDao userManageDao;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private UserManageInfoDao userManageInfoDao;
	@Autowired
	private BackLogServiceInterface backLogService;
	
	@Override
	public Map<String, Object> getUsers(String startTime, String endTime, Integer minAmount, Integer maxAmount,
			Integer searchType, String keyWord,Integer page,Integer size) {
		Map<String, Object> map = new HashMap<>();
		List<Usernobind> usernobinds = userManageDao.getUsers(startTime, endTime, minAmount, maxAmount, searchType, keyWord, page, size);
		long total = userManageDao.getUsersTotal(startTime, endTime, minAmount, maxAmount, searchType, keyWord);
		map.put("key", "success");
		map.put("users", usernobinds);
		map.put("total", total);
		return map;
	}

	@Override
	public List<Usernobind> getExportUsers(String startTime, String endTime, Integer minAmount, Integer maxAmount,
			Integer searchType, String keyWord) {
		List<Usernobind> usernobinds = null;
		usernobinds = userManageDao.getExportUsers(startTime, endTime, minAmount, maxAmount, searchType, keyWord);
		return usernobinds;
	}

	/**
	 * status 0:冻结 1:活动
	 */
	@Override
	public Map<String, Object> updateUserStatus(String backuserid,String userid, Integer status) {
		Map<String, Object> map = new HashMap<>();
		if(status == 0){
			long count = userManageDao.getUserUseingOrderCount(userid);
			if(count > 0){
				map.put("key", "error");
				map.put("msg", "用户存在未关闭订单，无法冻结");
				return map;
			}
		}
		int count = userManageDao.updateUserStatus(userid, status);
		if(count == 0){
			map.put("key", "error");
			map.put("msg", "用户状态修改失败");
			return map;
		}
		backLogService.addBackLog(backuserid, "冻结/解冻用户>> 用户id："+userid+" 操作："+(status == 1 ? "解冻" : "冻结"));
		map.put("key", "success");
		map.put("msg", "用户状态修改成功");
		return map;
	}

	@Override
	public Map<String, Object> getUserInfo(String userid) {
		Map<String, Object> map = new HashMap<>();
		User user = userRepository.getOne(userid);
		if(user == null){
			map.put("key", "error");
			map.put("msg", "用户不存在，查询失败");
			return map;
		}
		UserInfonobind userInfonobind = userManageInfoDao.getUserInfo(userid);
		if(userInfonobind == null){
			map.put("key", "error");
			map.put("msg", "用户不存在，查询失败");
			return map;
		}
		List<UserInfoEnterpriseVO> enterpriseVOs = userManageInfoDao.getUserEnterprise(userid);
		List<UserInfoOrdersVO> ordersVOs = userManageInfoDao.getUserOrders(userid);
		userInfonobind.setEnterprise(enterpriseVOs);
		userInfonobind.setOrders(ordersVOs);
		map.put("key", "success");
		map.put("user", userInfonobind);
		map.put("msg", "用户信息查询成功");
		return map;
	}
	
}
